<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="84px"
        :rules="formRules"
      >
        <el-form-item label="租户套餐" prop="typeId">
          <el-select
            v-model="formData.typeId"
            class="m-2"
            placeholder="请选择租户套餐"
            size="large"
          >
            <el-option
              v-for="item in tenantTypes"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="租户名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输租户名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="管理账号" prop="userName">
          <el-input
            v-model="formData.userName"
            placeholder="请输入管理账号"
            clearable
          />
        </el-form-item>
        <el-form-item label="管理昵称" prop="userName">
          <el-input
            v-model="formData.nickName"
            placeholder="请输入管理昵称"
            clearable
          />
        </el-form-item>
        <el-form-item v-if="formData.id == 0" label="管理密码" prop="password">
          <el-input
            v-model="formData.password"
            show-password
            clearable
            placeholder="请输入管理密码"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input
            v-model="formData.mobile"
            placeholder="请输入手机号"
            clearable
          />
        </el-form-item>
        <el-form-item label="账号数量" prop="accountCount">
          <el-input
            v-model="formData.accountCount"
            type="number"
            placeholder="请输入账号数量"
            clearable
          />
        </el-form-item>
        <el-form-item label="过期时间" prop="mobile">
          <el-date-picker
            v-model="formData.expireTime"
            type="date"
            placeholder="请选择过期时间"
            :default-value="new Date(2010, 9, 1)"
          />
        </el-form-item>
        <el-form-item label="租户状态">
          <el-switch
            v-model="formData.status"
            :active-value="0"
            :inactive-value="1"
          />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import { addTenant, editTenant } from "@/api/system/tenant";
import { useDictOptions } from "@/hooks/useDictOptions";
import { roleAll } from "@/api/perms/role";
import { postAll } from "@/api/org/post";
import { deptLists } from "@/api/org/department";
import feedback from "@/utils/feedback";
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const tenantTypes = ref(<any>[]);
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑管理员" : "新增管理员";
});

const formData = reactive({
  id: 0,
  name: "",
  nickName: "",
  userName: "",
  mobile: "",
  password: "",
  domain: "",
  status: 0,
  expireTime: "",
  typeId: undefined,
  accountCount: "",
  isMultipoint: 1,
});

const isRoot = computed(() => {
  return formData.id == 1;
});

const passwordConfirmValidator = (
  rule: object,
  value: string,
  callback: any
) => {
  if (formData.password) {
    if (!value) callback(new Error("请再次输入密码"));
    if (value !== formData.password) callback(new Error("两次输入密码不一致!"));
  }
  callback();
};
const checkMobile = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback();
  } else {
    const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    console.log(reg.test(value));
    if (reg.test(value)) {
      callback();
    } else {
      return callback(new Error("请输入正确的手机号"));
    }
  }
};
const formRules = reactive({
  name: [
    {
      required: true,
      message: "请输入租户名称",
      trigger: ["blur"],
    },
  ],
  mobile: [
    {
      required: true,
      message: "请输入手机号",
      trigger: ["blur"],
    },
    {
      validator: checkMobile,
      trigger: ["blur"],
    },
  ],
});

const handleSubmit = async () => {
  await formRef.value?.validate();
  mode.value == "edit" ? await editTenant(formData) : await addTenant(formData);
  popupRef.value?.close();
  feedback.msgSuccess("操作成功");
  emit("success");
};

const open = (type = "add", typeList: []) => {
  console.log("edit open typeList", typeList);

  mode.value = type;
  tenantTypes.value = typeList;
  popupRef.value?.open();
  console.log("edit open tenantTypes.value", tenantTypes.value);
};

/**
 * 设置属性
 */
const setFormData = async (row: any) => {
  const data = row;

  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
});
</script>
